﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.BO;

namespace PPAP.Web.DictionaryManage
{
    public partial class Entrys : PageBase
    {

        private DictionaryBO bo = new DictionaryBO();

        private string ids, parentIds;


        private void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面

            ids = Request.QueryString["ID"];

            parentIds = Request.QueryString["ParentID"];

            if (Page.IsPostBack) return;


            if (ids != null) //修改
            {
                this.btnSave.Text = "Update";
                PPAP.Model.Entity.DictionaryEntry data = bo.GetDictionaryEntry(Convert.ToInt32(ids));

                this.txtName.Text = data.Name;
                this.txtCode.Text = data.Code;
                //txtCode.Enabled = false;

                this.Type.Value = data.Type.ToString();

                this.ParentId.Value = data.ParentID.ToString();
                this.cbIsChecked.Checked = data.IsChecked;

                PPAP.Model.Entity.DictionaryEntry parent = bo.GetDictionaryEntry(data.ParentID);
                if (parent != null)
                    this.txtParentName.Text = parent.Name;
                else
                    this.txtParentName.Text = "Dictionary";

            }
            else if (parentIds != null) //添加
            {

                this.btnSave.Text = "Add";
                this.btnDel.Visible = false;

                PPAP.Model.Entity.DictionaryEntry parent = bo.GetDictionaryEntry(Convert.ToInt32(parentIds));

                if (parent != null)
                {
                    this.txtParentName.Text = parent.Name;
                    this.ParentId.Value = parentIds;
                }
                else
                    this.txtParentName.Text = "Dictionary";

            }
            else
            {
                this.txtParentName.Text = "Dictionary";
            }



        }

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void btnSave_Click(object sender, System.EventArgs e)
        {
            PPAP.Model.Entity.DictionaryEntry data = new PPAP.Model.Entity.DictionaryEntry();

            data.Name = this.txtName.Text.Trim();
            data.Code = this.txtCode.Text.Trim();
            data.IsChecked = this.cbIsChecked.Checked;

            if (this.ParentId.Value.Trim() != "")
                data.ParentID = Convert.ToInt32(this.ParentId.Value.Trim());

            string action = Request.QueryString["Action"];
            if (action != null) //添加
            {
                if (action == "AddFolder")
                    data.Type = 0;
                else
                    data.Type = 1;
            }
            else //修改
            {
                if (action == "AddFolder")
                    data.Type = 0;
                else
                    data.Type = 1;
            }


            try
            {
                if (ids != null)
                {
                    data.ID = Convert.ToInt32(ids);
                    bo.UpdateDictionaryEntry(data);
                }
                else
                {
                    bo.InsertDictionaryEntry(data);
                }
            }
            catch (Exception ex)
            {
                base.AlertMessage(ex.Message);
                return;
            }

            base.AlertRedirect("success！", "List.aspx?ID=" + data.ParentID);

        }

        private void btnDel_Click(object sender, System.EventArgs e)
        {
            //try
            //{
            //    bo.DeleteDictionaryEntry( Convert.ToInt32(id) ) ;
            //}
            //catch( Exception ex )
            //{
            //    base.AlertMessage( ex.Message ) ;
            //    return ;
            //}

            //base.AlertRedirect("Operation success！", "List.aspx?ID=" + this.ParentId.Value);

        }

        private void btnCheckCode_Click(object sender, System.EventArgs e)
        {
            string code = this.txtCode.Text.Trim();

            if (code == "")
            {
                base.AlertMessage("Code can't for empty！");
                return;
            }

            bool isOnlyOne = false;

            int count = bo.GetCountByCode(code);

            if ((ids == null && count == 0) || (ids != null && count <= 1))
                isOnlyOne = true;

            if (isOnlyOne)
                base.AlertMessage("The only");
            else
                base.AlertMessage("Not only");



        }

        protected void btnSave_Click1(object sender, EventArgs e)
        {
            btnSave_Click(null, null);
        }




    }
}
